package com.hq.controller;

import com.hq.model.Guide;
import com.hq.model.Passenger;
import com.hq.model.Root;
import com.hq.model.ScenicSpot;
import com.hq.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Resource
    AdminService adminService;

    //旅游景点管理界面
    @RequestMapping("/scenicSpot")
    String scenicSpot(Model model){
        List<ScenicSpot> scenicSpots = adminService.selectAll();
        model.addAttribute("ScenicSpot",scenicSpots);
        return "/back/admin/scenicSpot";
    }

    //添加景点页面
    @RequestMapping("/addPage")
    String addPage(){
        return "/back/admin/addScenicSpotPage";
    }

    //添加景点
    @RequestMapping("/addOne")
    String addOne(ScenicSpot scenicSpot){
        adminService.addOne(scenicSpot);
        return "redirect:/admin/scenicSpot";
    }

    //旅游景点界面跳转到修改界面
    @RequestMapping("/updatePage/{id}")
    String updatePage(@PathVariable("id") int id, Model model){
        ScenicSpot byId = adminService.selectId(id);
        model.addAttribute("byId",byId);
        return "/back/admin/update";
    }

    //修改景点
    @RequestMapping("/updateScenicSpot")
    String updateScenicSpot(ScenicSpot scenicSpot){
        adminService.updateScenicSpot(scenicSpot);
        return "redirect:/admin/scenicSpot";
    }

    //删除景点
    @RequestMapping("/delete/{id}")
    String delete(@PathVariable("id") int id){
        adminService.deleteOne(id);
        return "redirect:/admin/scenicSpot";
    }

    //导游管理界面
    @RequestMapping("/guide")
    String guide(Model model){
        List<Guide> guide = adminService.selectGuide();
        model.addAttribute("guideList",guide);
        return "/back/admin/guide";
    }

    //添加导游页面
    @RequestMapping("/addGuidePage")
    String addGuidePage(){
        return "/back/admin/addGuidePage";
    }

    //添加导游
    @RequestMapping("/addGuide")
    String addGuide(Root root){
        adminService.addGuide(root);
        adminService.addUsername(root.getUsername());
        return "redirect:/admin/guide";
    }

    //修改导游页面
    @RequestMapping("/updateGuidePage/{id}")
    String updateGuidePage(@PathVariable("id") int id, Model model){
        Guide byId = adminService.selectGuideId(id);
        model.addAttribute("byId",byId);
        return "/back/admin/updateGuide";
    }

    //修改导游
    @RequestMapping("/updateGuide")
    String updateGuide(Guide guide){
        adminService.updateGuide(guide);
        return "redirect:/admin/guide";
    }

    //删除导游
    @RequestMapping("/deleteGuide/{username}")
    String deleteGuide(@PathVariable("username") String username){
        adminService.deleteGuide(username);
        adminService.deleteRoot(username);
        return "redirect:/admin/guide";
    }

    //跳转用户管理界面
    @RequestMapping("/passenger")
    String passenger(Model model){
        List<Passenger> passengerList = adminService.selectPassenger();
        model.addAttribute("passengerList",passengerList);
        return "/back/admin/passenger";
    }

    //修改用户页面
    @RequestMapping("/updatePassengerPage/{id}")
    String updatePassengerPage(@PathVariable("id") int id, Model model){
        Passenger byId = adminService.selectPassengerId(id);
        model.addAttribute("byId",byId);
        return "/back/admin/updatePassenger";
    }

    //修改用户
    @RequestMapping("/updatePassenger")
    String updatePassenger(Passenger passenger){
        adminService.updatePassenger(passenger);
        return "redirect:/admin/passenger";
    }

    //删除景点
    @RequestMapping("/deletePassenger/{id}")
    String deletePassenger(@PathVariable("id") int id){
        adminService.deletePassenger(id);
        return "redirect:/admin/passenger";
    }
}
